<?php

namespace app\index\controller\my;

use app\index\model\UM as UM;
use app\index\service\MemberService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\Request as Request;

/**
 * Member 控制器
 * Class Member
 * @package app\my\controller
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class Member
{
    // 请求参数
    private Request $request;

    // service
    private MemberService $service;

    public function __construct(Request $request, MemberService $memberService)
    {
        $this->request = $request;
        $this->service = $memberService;
    }

    /**
     * 详情
     * @param \app\index\model\UM|null $user
     * @return ApiResponse
     * @url /my/member/read
     */
    public function read(?UM $user): ApiResponse
    {
        $id = $user ? $user->id : null;

        if (!is_id($id)) {
            return ApiResponse::success(null);
        }

        return ApiResponse::success($this->service->read($user, $id));
    }

    /**
     * 详情
     * @param \app\index\model\UM|null $user
     * @return ApiResponse
     * @url /my/member/save
     */
    public function save(UM $user): ApiResponse
    {
        $this->service->modifyByMember($user, $user->id, $this->request->param());

        return ApiResponse::success(null);
    }
}
